package com.perceptivesoftware.licensing.client.common.util;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.event.dom.client.KeyCodes;

public class PerceptiveLicensingMessageBox
{
	public static MessageBox prompt(final String title, final String msg, final Listener<MessageBoxEvent> callback) {
		final MessageBox m = MessageBox.prompt(title, msg, callback);
		m.getTextBox().addKeyListener(new KeyListener()
		{
			@Override
			public void componentKeyPress(final ComponentEvent event)
			{
				super.componentKeyPress(event);
				if (event.getKeyCode() == KeyCodes.KEY_ENTER)
				{
					m.getDialog().getButtonById(Dialog.OK).fireEvent(Events.Select);
				}
			}
		});
		return m;
	}

	public static MessageBox confirm(final String title, final String msg, final Listener<MessageBoxEvent> callback) {
		final MessageBox box = new MessageBox();
		box.setTitle(title);
		box.setMessage(msg);
		box.addCallback(callback);
		box.setIcon(MessageBox.QUESTION);
		box.setButtons(Dialog.YESNOCANCEL);
		box.show();
		return box;
	}
}
